import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2020/10/20 14:28
 */
public class Test {
    public static void main(String[] args) throws IOException {
        //创建一个工作蒲
        Workbook workbook = new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("表的名字");

        sheet.setColumnWidth(0,15000);

        //创建一个style
        CellStyle style = workbook.createCellStyle();
        //设置水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //设置黄色的背景颜色
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        //设置背景颜色为铺满

        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //设置字体
        HSSFFont font = (HSSFFont) workbook.createFont();
        font.setFontName("Arial");
        font.setFontHeightInPoints((short) 10);
        font.setFontHeight((short)500);
        font.setBold(true);
        font.setColor(IndexedColors.RED.getIndex());
        style.setFont(font);


        //创建一个写入流
        try (FileOutputStream outputStream = new FileOutputStream("a.xls")){


            Row row0 = sheet.createRow(0);
            Cell cell00 = row0.createCell(0);
            cell00.setCellStyle(style);
            cell00.setCellValue("这个是有背景颜色的");

            Cell cell01 = row0.createCell(1);
            cell01.setCellValue("这个是没有背景颜色的");


            Row row1 = sheet.createRow(1);
            Cell cell_10 = row1.createCell(0);
            cell_10.setCellFormula("HYPERLINK(\"https://www.baidu.com\", \"百度一下，你就知道\")");

            //这个一定要记得写在后面，要不然会出现无法写入的问题（打开excel为空）
            workbook.write(outputStream);
        }
        System.out.println("执行完成。。。。");
    }
}
